package com.ccp.dev.qbdms.util;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.DateFormatUtil;
import com.ccp.dev.core.util.TimeUtil;
import com.ccp.dev.qbdms.expimp.annotation.Record;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.Date;

/**
 * 构造两个对象属性值变化的日志记录
 */
public class StructureModifyUtil {

    public static String getModifyInfo(Object oldData, Object newData) {
        try {
            StringBuffer content = new StringBuffer();

            Class clazz = newData.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {

                // with modifiers "private"
                ReflectionUtils.makeAccessible(field);
                Record recordAnnotation = field.getAnnotation(Record.class);
                if (recordAnnotation == null) {
                    continue;
                }
                Object newValue = field.get(newData);
                Object oldValue = field.get(oldData);
                if (BeanUtils.isNotEmpty(newValue) && !newValue.equals(oldValue)) {
                    content.append("将【");
                    String fieldName = recordAnnotation.value() == null ? field.getName() : recordAnnotation.value();
                    content.append(fieldName);
                    content.append("】");
                    content.append("从'");
                    //默认值、日期等特殊处理
                    String[] defaultValueArray = recordAnnotation.defaultValue();
                    oldValue = checkDefaultVal(oldValue, defaultValueArray);
                    newValue = checkDefaultVal(newValue, defaultValueArray);
                    if (field.getType().isInstance(new Date())) {
                        oldValue = TimeUtil.getDateTimeString(new Date(String.valueOf(field.get(oldData))));
                    }
                    if (newValue instanceof Date) {
                        newValue = DateFormatUtil.formatDate((Date) newValue);
                    }

                    content.append(String.valueOf(oldValue));
                    content.append("'改为'");
                    content.append(String.valueOf(newValue));
                    content.append("';");
                }
            }
            return content.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }

    public static Object checkDefaultVal(Object fieldVal, String[] defaultValueArray) {
        if (defaultValueArray != null && defaultValueArray.length > 0) {
            Object defaultObj = "";
            // 设置默认值
            for (int k = 0; k < defaultValueArray.length; k++) {
                try {
                    String[] defaultValue = defaultValueArray[k].split(":");
                    if (defaultValue[0].equals(fieldVal)) {
                        defaultObj = defaultValue[1];
                        break;
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return defaultObj;
        } else {
            return fieldVal;
        }
    }
}
